package vn.edu.saigontech.ncms.web.receipt;

import java.util.Date;

import vn.edu.saigontech.ncms.model.enumeration.Currency;
import vn.edu.saigontech.ncms.model.info.InternalReceipt;
import vn.edu.saigontech.ncms.model.info.ReceiptInfo;
import vn.edu.saigontech.ncms.model.system.Money;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.InternalReceiptService;
import vn.edu.saigontech.ncms.service.ReceiptInfoService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class InternalReceiptAction extends BaseAction {
	
	private Long id;
	private Long[] ids;
	private String number;
	private String date;
	private String name;
	private String dob;
	private Double fee;
	private boolean active;
	private Long cashierId;
	private String comment;
	
	public String showAdd() {
		REQUEST().setAttribute("users", UserService.INSTANCE.findAll());
		REQUEST().setAttribute("cashiers", UserService.INSTANCE.findByRole("Consulting"));
		return SUCCESS;
	}
	
	public String add() {
		User cashier = UserService.INSTANCE.findById(cashierId);
		Date date = DateUtil.fromString(this.date, this.i18n("format.date"));
		String internalNotation = "BS";
		String receiptNotation = "";
		String number = ReceiptInfoService.INSTANCE.generateReceiptInfoName(ReceiptInfo.HP, 
				internalNotation, receiptNotation, DateUtil.getYear(date));
		
		InternalReceipt entity = new InternalReceipt();
		entity.setName(this.name);
		entity.setActive(true);
		entity.setCashier(cashier);
		entity.setComment(this.comment);
		entity.setDate(date);
		entity.setFee(new Money(this.fee, Currency.VND));
		entity.setNumber(number);
		
		entity = InternalReceiptService.INSTANCE.persist(entity);
		
		if(Checker.isNotNull(entity)) {
			this.addLoggingDetail(BaseAction.ADD, "logging.internalreceipt.add", this.name, entity.getNumber());
		}
		
		this.id = entity.getId();
		return SUCCESS;
	}
	
	public String info() {
		REQUEST().setAttribute("receipt", InternalReceiptService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String list() {
		REQUEST().setAttribute("receipts", InternalReceiptService.INSTANCE.findAll());
		return SUCCESS;
	}
	
	public String delete() {
		InternalReceipt receipt = InternalReceiptService.INSTANCE.findById(this.id);
		String name = receipt.getName();
		String number = receipt.getNumber();
		if(InternalReceiptService.INSTANCE.deleteById(this.id)) {
			this.setKeyMessage("confirm.deleteSuccessfully", "receipt");
			this.addLoggingDetail(BaseAction.DELETE, "logging.internalreceipt.delete", name, number);
		}
		else {
			this.setKeyMessage("confirm.deleteUnsuccessfully", "receipt");
		}
		return SUCCESS;
	}
	
	public String deleteMore() {
		if(!Checker.isNull(this.ids)) {
			for (Long each : this.ids) {
				InternalReceipt rec = InternalReceiptService.INSTANCE.findById(each);
				String name = rec.getName();
				String number = rec.getNumber();
				if(InternalReceiptService.INSTANCE.deleteById(each)) {
					this.addLoggingDetail(BaseAction.DELETE, "logging.internalreceipt.delete", name, number);
				}
			}
		}
		return SUCCESS;
	}
	
	public String print() {
		InternalReceipt receipt = InternalReceiptService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("receipt", receipt);
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getStudentDateOfBirth() {
		return dob;
	}

	public void setStudentDateOfBirth(String studentDateOfBirth) {
		this.dob = studentDateOfBirth;
	}

	public Double getFee() {
		return fee;
	}

	public void setFee(Double fee) {
		this.fee = fee;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public Long getCashierId() {
		return cashierId;
	}

	public void setCashierId(Long cashierId) {
		this.cashierId = cashierId;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}
}